package cn.itcast.zuoyebang.utils;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Utils {
    public static String getMD5(String text){
        String md5 = "";
        MessageDigest digest = null;
        try {
            digest = MessageDigest.getInstance("md5");
            byte[] result = digest.digest(text.getBytes());
            for(byte b: result){
                //b转换成int
                int num = b & 0xff;
                String str = Integer.toHexString(num);
                if(str.length() == 1){
                    str = "0" + str;
                }
                md5 = md5 + str;
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        return md5;
    }
}
